var prefs = function() {

	var prefBranch;
	var stocks = [];

	var getPrefBranch = function() {
		if (!prefBranch) {
			prefBranch = Components.classes['@mozilla.org/preferences-service;1'].getService();
			prefBranch = prefBranch.QueryInterface(Components.interfaces.nsIPrefBranch);
		}
		return prefBranch;
	};

	var loadMenuPopup = function(setting) {
		var target = document.getElementById(setting.target);
		var popup = document.createElement("menupopup");
		var options = (setting.options instanceof Array)
			? setting.options
			: $H(setting.options);
		options.each(function(option) {
			var item = document.createElement("menuitem");
			if (setting.options instanceof Array) {
				item.setAttribute("label", option);
			} else {
				item.setAttribute("label", option.key);
				item.setAttribute("value", option.value);
			}
			popup.appendChild(item);
		});
		target.appendChild(popup);
		target.selectedIndex = prefs.getIntPref(setting.target, setting.default_value || 0);
	};

	var saveMenuPopup = function(setting) {
		var target = document.getElementById(setting.target);
		prefs.setIntPref(setting.target, target.selectedIndex);
	};

	var menupopup = {
		load: loadMenuPopup,
		save: saveMenuPopup,
		options: []
	};

	var getLinkLocations = function() {
		var locale = document.getElementById("string-bundle");
		return [
				locale.getString("options.CURRENT_TAB"),
				locale.getString("options.NEW_TAB"),
				locale.getString("options.NEW_WINDOW"),
				locale.getString("options.UNFOCUSED_TAB")
				];
	};

	var getRefreshIntervals = function() {
		return $R(1, 60).toArray();
	};

	return {

		CURRENT_TAB: 0,
		NEW_TAB: 1,
		NEW_WINDOW: 2,
		UNFOCUSED_TAB: 3,

		getStocks: function() {
			//TODO: there should be a better way to get a sorted list.
			if (stocks.length == 0) {
				// get stock list from properties file with singleton pattern.
				var enumerator = document.getElementById("stock-bundle").strings;
				while (enumerator.hasMoreElements()) {
					var property = enumerator.getNext().QueryInterface(Components.interfaces.nsIPropertyElement);
					stocks.push(property.key);
				}
				stocks.sort();
			}
			return stocks;
		},

		settings: {
			load_location: Object.extend({target: 'prefs.load_location'}, menupopup),
			default_stock: Object.extend({target: 'prefs.default_stock'}, menupopup),
			refresh_interval: Object.extend({target: 'prefs.refresh_interval', default_value: 2}, menupopup)
		},

		setIntPref: function(name, value) {
			getPrefBranch().setIntPref("imkbizle." + name, value);
		},

		getIntPref: function(name, defaultval) {
			var val;
			try {
				val = getPrefBranch().getIntPref("imkbizle." + name);
			} catch(err) {
				val = defaultval;
			}
			return val;
		},

		save: function() {
			$H(prefs.settings).each(function(item) {
				item.value.save(item.value);
			});
		},

		load: function() {
			prefs.settings.load_location.options = getLinkLocations();
			prefs.settings.refresh_interval.options = getRefreshIntervals();
			prefs.settings.default_stock.options = prefs.getStocks();
			$H(prefs.settings).each(function(item) {
				item.value.load(item.value);
			});
		}
	};
}()